package com.matt.repository;

import com.matt.domain.ChildActivity;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.List;

public class ChildActivitySpecification {

//    public static Specification<ChildActivity> buildSpecification(String areaId, List<String> classIds) {
//        return new Specification<ChildActivity>() {
//            private static final long serialVersionUID = 1L;
//
//            @Override
//            public Predicate toPredicate(Root<ChildActivity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
//                List<Predicate> predicates = new ArrayList<>();
//
//                if (areaId != null && !areaId.isEmpty()) {
//                    predicates.add(criteriaBuilder.equal(root.get("areaId"), areaId));
//                }
//
//                if (classIds != null && !classIds.isEmpty()) {
//                    for (String classId : classIds) {
//                        predicates.add(criteriaBuilder.like(root.get("classIds"), "%" + classId + "%"));
//                    }
//                }
//
//                // 添加排序
//                query.orderBy(criteriaBuilder.desc(root.get("createTime")));
//
//                return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
//            }
//        };
//    }

//    public static Specification<ChildActivity> buildSpecification(String areaId, List<String> classIds) {
//        return (root, query, criteriaBuilder) -> {
//            List<Predicate> predicates = new ArrayList<>();
//
//            if (areaId != null && !areaId.isEmpty()) {
//                predicates.add(criteriaBuilder.equal(root.get("areaId"), areaId));
//            }
//
//            if (classIds != null && !classIds.isEmpty()) {
//                List<Predicate> classPredicates = new ArrayList<>();
//                for (String classId : classIds) {
//                    String trimmedId = classId.trim();
//                    Predicate findInSet = criteriaBuilder.greaterThan(
//                            criteriaBuilder.function("FIND_IN_SET", Integer.class,
//                                    criteriaBuilder.literal(trimmedId), root.get("classIds")),
//                            0
//                    );
//                    classPredicates.add(findInSet);
//                }
//                predicates.add(criteriaBuilder.or(classPredicates.toArray(new Predicate[0])));
//            }
//
//            query.orderBy(criteriaBuilder.desc(root.get("createTime")));
//            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
//        };
//    }

    public static Specification<ChildActivity> buildSpecification(String areaId, List<String> classIds) {
        return (root, query, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<>();

            if (areaId != null && !areaId.isEmpty()) {
                predicates.add(criteriaBuilder.equal(root.get("areaId"), areaId));
            }

            if (classIds != null && !classIds.isEmpty()) {
                Expression<String> classIdsExpression = root.get("classIds");
                Predicate findInSetPredicate = criteriaBuilder.or(classIds.stream()
                        .map(id -> criteriaBuilder.like(classIdsExpression, "%" + id.trim() + "%"))
                        .toArray(Predicate[]::new));
                predicates.add(findInSetPredicate);
            }

            query.orderBy(criteriaBuilder.desc(root.get("createTime")));
            return criteriaBuilder.and(predicates.toArray(new Predicate[0]));
        };
    }
}